package com.example.demo.util;

import com.example.demo.entity.Dakamingxi;
import com.example.demo.finalUtils.FinalUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.demo.finalUtils.Config.*;
@Component
public class RestHttp {
    @Autowired
    FinalUtils finalUtils;
    @Value("${net.tenantName}")
    private  String tenantName;
    @Value("${net.tenantPwd}")
    private  String tenantPwd;
    @Value("${net.host}")
    private  String host;
    @Value("${net.port}")
    private String port;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 华为云管理理
     * @return
     * @throws IOException
     * @throws ParseException
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public String restHttp() {
        //设置POST的URL,Header和超时时间
        String postTokenUrl = "https://" + host + ":" + port + POST_TOKEN_URL;
        HttpHeaders httpHeaders=new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        httpHeaders.setContentType(type);
        httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
        Map<String, String> map= new HashMap<>();
        map.put(USERNAME, tenantName);
        map.put(PASSWORD, tenantPwd);
        HttpEntity<Map> formEntity = new HttpEntity<Map>(map, httpHeaders);
        ResponseEntity<String> result =restTemplate.postForEntity(postTokenUrl, formEntity, String.class);
        String D= getSite(result.getBody());
        System.out.println(result.getBody());
        return result.getBody();
    }

    private String getSite(String body) {
        String getSitesUrl = "https://" + host + ":" + port + GET_SITES_URL;
        JSONObject jsonResponse = new JSONObject(body);
        JSONObject data= (JSONObject) jsonResponse.get("data");
        JSONObject jsonArray=new JSONObject(data);
        String token_id= (String) data.get("token_id");


        return null;
    }
}
